home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / FILER / TARSRC.SPK / c / dir < prev    next >
Text File  |  1995-03-31  |  8KB  |  337 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "tar.h"
  4. #include "dir.h"
  5.  
  6.  
  7. #if !defined(WITH_PWD)
  8. char *DownPath(char *Path, char *DownTo, char **PathSteps, int *Depth) {
  9.   char *cp;
  10.  
  11.   PathSteps = realloc(PathSteps, ++(*Depth));
  12.   cp = Path;
  13.   while (*cp) {
  14.     ++cp;
  15.   }
  16.   PathSteps[(*Depth) - 1] = cp;
  17.   strcat(cp, DownTo);
  18. } /* DownPath */
  19.  
  20.  
  21. char *UpPath(char *Path, char **PathSteps, int *Depth) {
  22.   if (*Depth > 0) {
  23.     *(PathSteps[--(*Depth)]) = '\0';
  24.     PathSteps = realloc(PathSteps, *Depth);
  25.   } else {
  26.     fprintf(stderr,"tar: warning: trying to move further up than top of path!\n");
  27.   }
  28.   return Path;
  29. } /* UpPath */
  30. #endif
  31.  
  32.  
  33. #if WITH_PWD
  34. void ChangeCurrentDir(char *Name) {
  35.   while (!chkos(os_swi2(os_X | OS_FSControl,0,(int)Name)));
  36. } /* ChangeCurrentDir */
  37. #endif
  38.  
  39.  
  40. static char *ReadFSInfo(int Action, int LengthPos, int NameStart) {
  41.   os_gbpbstr Blk;
  42.   static char Data[256];
  43.  
  44.   do {
  45.     Blk.action = Action;
  46.     Blk.file_handle = 0;
  47.     Blk.data_addr = (void *)Data;
  48.     Blk.buf_len = 256;
  49.   } while (!chkos(os_gbpb(&Blk)));
  50.   Data[Data[LengthPos] + NameStart] = '\0';
  51.   return &Data[NameStart];
  52. } /* ReadFSInfo */
  53.  
  54.  
  55. #if WITH_PWD
  56. int Unset;
  57.  
  58.  
  59. int CurrentDirUnset() {
  60.   return (strcmp(ReadFSInfo(6,1,2), "\"Unset\"") == 0) ? 1 : 0;
  61. } /* CurrentDirUnset */
  62.  
  63.  
  64. char *GetCurrentDir(char *CurrentDirName) {
  65.   char *cp;
  66.   os_regset Regs;
  67.   int FSNumber;
  68.   char FSName[256];
  69.  
  70.   if (UseCanonicalisedPaths) {
  71.     Regs.r[0] = 37;
  72.     Regs.r[1] = (int)"@";
  73.     Regs.r[2] = (int)FSName;
  74.     Regs.r[3] = 0;
  75.     Regs.r[4] = 0;
  76.     Regs.r[5] = sizeof(FSName);
  77.     if (os_swix(OS_FSControl, &Regs) == NULL) {
  78.       strcpy(CurrentDirName, FSName);
  79.       return CurrentDirName;
  80.     }
  81.   }
  82.  
  83.   *CurrentDirName = '\0';
  84.   do {
  85.     cp = ReadFSInfo(6,1,2);
  86.     if (*CurrentDirName) {
  87.       strcat(cp,".");
  88.       strcat(cp,CurrentDirName);
  89.     }
  90.     strcpy(CurrentDirName,cp);
  91.     ChangeCurrentDir("\\");
  92.     ChangeCurrentDir("\\.^");
  93.   } while (*CurrentDirName != '$' && *CurrentDirName != 0);
  94.   if (*CurrentDirName == 0) {
  95.     return NULL;
  96.   }
  97.   cp = ReadFSInfo(5,0,1);
  98.   strcat(cp,".");
  99.   strcat(cp,CurrentDirName);
  100.   strcpy(CurrentDirName,cp);
  101.   do {
  102.     Regs.r[0] = 0;
  103.     Regs.r[1] = 0;
  104.     Regs.r[2] = 0;
  105.   } while (!chkos(os_args(&Regs)));
  106.   FSNumber = Regs.r[0];
  107.   do {
  108.     Regs.r[0] = 33;
  109.     Regs.r[1] = FSNumber;
  110.     Regs.r[2] = (int)FSName;
  111.     Regs.r[3] = 256;
  112.   } while (!chkos(os_swix(OS_FSControl,&Regs)));
  113.   strcat(FSName,"::");
  114.   strcat(FSName,CurrentDirName);
  115.   strcpy(CurrentDirName,FSName);
  116.   ChangeCurrentDir("\\");
  117.   ChangeCurrentDir(CurrentDirName);
  118.   return(CurrentDirName);
  119. } /* GetCurrentDir */
  120. #endif
  121.  
  122.  
  123. int MakeDir(char *Name) {
  124.   return(((os_swi2(os_X | OS_File, 8, (int)Name)) != NULL) ? -1 : 0);
  125. } /* MakeDir */
  126.  
  127.  
  128. static char Buffer[MAXPATHLEN];
  129.  
  130. int name_to_unix(char *Name) {
  131.   char *LastSep, *cp, *Ext;
  132.  
  133.   LastSep = NULL;
  134.   Ext = Name;
  135.   cp = Name;
  136.   while (*cp) {
  137.     if (*cp == FS_DIRSEP) {
  138.       if (LastSep != NULL)
  139.         Ext = LastSep + 1;
  140.       LastSep = cp;
  141.       *cp = UNIX_DIRSEP;
  142.     } else if (*cp == UNIX_DIRSEP)
  143.       *cp = FS_DIRSEP;
  144.     else if (ConvertExclamationMark && *cp == '!')
  145.       *cp = '_';
  146.     cp++;
  147.   }
  148.   if (SwapExtensionToDir && LastSep != NULL) {
  149.     *LastSep = '\0';
  150.     if (strlen(Ext) > MaxExtLength)
  151.       *LastSep = UNIX_DIRSEP;
  152.     else {
  153.       if (*(LastSep + 1) == '\0')
  154.         return 0;
  155.       strcpy(Buffer,Ext);
  156.       *Ext = 0;
  157.       strcpy(Ext,LastSep + 1);
  158.       strcat(Name,".");
  159.       strcat(Name,Buffer);
  160.     }
  161.   }
  162.   return 1;
  163. } /* name_to_unix */
  164.  
  165.  
  166. static int chktruncate(char *leaf, char *rest) {
  167.   int len, n, i;
  168.   char *cp, *cp2;
  169.  
  170.   n = 0;
  171.   if (MaxLeafLength > 0 &&
  172.       (len = strlen(leaf)) > MaxLeafLength) {
  173.     fprintf(stderr,"tar: shorten: %s -> ",leaf);
  174.     cp = leaf;
  175.     while (*cp && len > MaxLeafLength) {
  176.       switch (*cp) {
  177.         case 'a':
  178.         case 'e':
  179.         case 'i':
  180.         case 'o':
  181.         case 'u':
  182.         case '-':
  183.         case '_':
  184.           if (cp != leaf) {
  185.             n += 1;
  186.             len -= 1;
  187.             cp2 = cp + 1;
  188.             while (*cp2) {
  189.               *(cp2 - 1) = *cp2;
  190.               cp2++;
  191.             }
  192.             *(cp2 - 1) = 0;
  193.             break;
  194.           }
  195.         default:
  196.           cp++;
  197.           break;
  198.       }
  199.     }
  200.     len = strlen(leaf);
  201.     i = 1;
  202.     while (len > MaxLeafLength) {
  203.       strcpy(leaf + i, leaf + i + 1);
  204.       n += 1;
  205.       len -= 1;
  206.       *(leaf + len) = 0;
  207.       i += 2;
  208.       if (i > len) {
  209.         i = 1;
  210.       }
  211.     }
  212.     *(leaf + MaxLeafLength) = 0;
  213.     fprintf(stderr,"%s\n",leaf);
  214.     if (n > 0 && rest != NULL) {
  215.       sprintf(leaf, "%s%c%s", leaf, FS_DIRSEP, rest);
  216.     }
  217.   }
  218.   return(n);
  219. } /* chktruncate */
  220.  
  221.  
  222. void name_to_fs(char *name) {
  223.   char *cp, *leaf, *ext;
  224.   int extlen;
  225.  
  226.   Buffer[0] = '\0';
  227.   leaf = name;
  228.   ext = NULL;
  229.  
  230.   for (cp = name; *cp; cp++) {
  231.     if (*cp == UNIX_DIRSEP) {
  232.       *cp = 0;
  233.       cp -= chktruncate(leaf, cp + 1);
  234.       if ((SwapExtensionToDir || SwapInWholePath) && Buffer[0] != '\0') {
  235.         if (SwapInWholePath) {
  236.         }
  237.         Buffer[0] = '\0';
  238.       }
  239.       *cp = FS_DIRSEP;
  240.       leaf = cp + 1;
  241.     } else if (*cp == FS_DIRSEP) {
  242.       if (SwapExtensionToDir || SwapInWholePath) {
  243.         *cp = '\0';
  244.         if (ConvertCompressExtension && *(cp + 1) && strcmp(cp + 1, "Z") == 0) {
  245.           if (Buffer[0] == '\0')
  246.             strcpy(Buffer,leaf);
  247.           strcat(Buffer,CompressExt);
  248.           cp++;
  249.         } else {
  250.           strcpy(Buffer,leaf);
  251.           chktruncate(Buffer, NULL);
  252.           if (PeriodSlashConversion)
  253.             *cp = UNIX_DIRSEP;
  254.           else
  255.             *cp = FS_DIRSEP;
  256.           ext = cp + 1;
  257.         }
  258.       } else if (PeriodSlashConversion) {
  259.         *cp = UNIX_DIRSEP;
  260.       } else {
  261.         *cp = 0;
  262.         cp -= chktruncate(leaf, cp + 1);
  263.         *cp = FS_DIRSEP;
  264.         leaf = cp + 1;
  265.       }
  266. /*
  267.     } else if (PeriodSlashConversion && *cp == '_') {
  268.       *cp = 0;
  269.       strcpy(Buffer,name);
  270.       strcat(Buffer,cp+1);
  271.       strcpy(name,Buffer);
  272.       cp--;
  273. */
  274.     }
  275.   }
  276.   if ((SwapExtensionToDir || SwapInWholePath) && Buffer[0] != '\0') {
  277.     if (ext != NULL) {
  278.       chktruncate(ext, NULL);
  279.       extlen = strlen(ext);
  280.       strcpy(leaf,ext);
  281.       leaf += extlen;
  282.       *leaf++ = '.';
  283.     }
  284.     strcpy(leaf,Buffer);
  285.     Buffer[0] = '\0';
  286.   }
  287.   chktruncate(leaf,(char *)NULL);
  288. } /* name_to_fs */
  289.  
  290.  
  291. static char NameBuffer[9];
  292.  
  293. char *FileTypeName(int FileType) {
  294.   char *cp;
  295.   os_regset Regs;
  296.  
  297.   Regs.r[0] = 18;
  298.   Regs.r[2] = FileType;
  299.   do {
  300.   } while (!chkos(os_swix(OS_FSControl, &Regs)));
  301.   cp = NameBuffer;
  302.   *(cp++) = tolower(Regs.r[2] & 0xFF);
  303.   *(cp++) = tolower((Regs.r[2] >>  8) & 0xFF);
  304.   *(cp++) = tolower((Regs.r[2] >> 16) & 0xFF);
  305.   *(cp++) = tolower((Regs.r[2] >> 24) & 0xFF);
  306.   *(cp++) = tolower(Regs.r[3] & 0xFF);
  307.   *(cp++) = tolower((Regs.r[3] >>  8) & 0xFF);
  308.   *(cp++) = tolower((Regs.r[3] >> 16) & 0xFF);
  309.   *(cp++) = tolower((Regs.r[3] >> 24) & 0xFF);
  310.   while (*(--cp) == ' ') ;
  311.   *(++cp) = 0;
  312.   return NameBuffer;
  313. } /* FileTypeName */
  314.  
  315.  
  316. void SetFileType(char *FileName, int FileType) {
  317.   os_regset Regs;
  318.  
  319.   Regs.r[0] = 18;
  320.   Regs.r[1] = (int)FileName;
  321.   Regs.r[2] = FileType;
  322.   chkos(os_swix(OS_File, &Regs));
  323. } /* SetFileType */
  324.  
  325.  
  326. void SetAttributes(int Cmd, char *FileName,
  327.                    int Attributes, int LoadAddress, int ExecAddress) {
  328.   os_regset Regs;
  329.  
  330.   Regs.r[0] = Cmd;
  331.   Regs.r[1] = (int)FileName;
  332.   Regs.r[2] = LoadAddress;
  333.   Regs.r[3] = ExecAddress;
  334.   Regs.r[5] = Attributes;
  335.   chkos(os_swix(OS_File, &Regs));
  336. } /* SetAttributes */
  337.